import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.ArrayList;

/**
 * Write a description of class Play here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Play extends World
{

    /**
     * Constructor for objects of class Play.
     * 
     */
    public static Player p = new Player();
    private static Star[] s = new Star[MainMenu.stars];
    public static MousePointer pnt = new MousePointer();
    public static TractorBeam beam;
    private boolean isTractor = false;
    private boolean pressSwitch = false;
    public static HullPiece16 piece;

    public static double Mxr = 0;
    public static double Myr = 0;
    private static int step = 0;

    private static int UlX = 0;
    private static int UlY = 0;//upper left X/Y

    private static final int WorldBorderX = 100000;//that's 100,000
    private static final int WorldBorderY = 100000;//these don't do much now.
    
    public static ArrayList<Attachable> addons = new ArrayList<Attachable>();
    public Play()
    {    
        
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1); 
        GreenfootImage i = new GreenfootImage(1000,600);
        i.setColor(Color.BLACK);
        i.fill();
        setBackground(i);

        populate();
        Attachable.getAllAttachmentPoints();

        prepare();
    }

    private void populate(){
        for(int x = 0; x < MainMenu.stars; x++){
            s[x] = new Star();
        }    

        for(int x = 0; x < MainMenu.stars; x++){
            addObject(s[x], MainMenu.coords[0][x],MainMenu.coords[1][x]);
        }
        addObject(p, 500,300);

        addObject(pnt,1,1);
        beam = new TractorBeam(p);
        piece = new HullPiece16(.5);
        addObject(piece, 500,200);

    }
    public Player getPlayer(){
        return p;
    }

    /*public static Integer[] getMovement(){
    Integer[] i = {Mx, My};
    return i;
    }*/
    public static void addMovement(double x, double y){
        if(Math.abs(Mxr)<10)
        {
            Mxr += x;
        }else{
            if(Mxr > 0){
                Mxr--;
            }else{
                Mxr++;
            }

        }
        if(Math.abs(Myr)<10)
        {
            Myr += y;
        }else{
            if(Myr > 0){
                Myr--;
            }else{
                Myr++;
            }
        }

    }

    @Override
    public void act(){

        double Mx = Math.round(Mxr);
        double My = Math.round(Myr);

        piece.coordUpdate(Mx,My);
     
        UlX += Mx;
        UlY += My;

        //System.out.print(X+" , "+ Y+" ");
        //System.out.println((getTrueX()+500)+" , "+(getTrueY()+300));

        p.coordUpdate(Mx,My);
        //System.out.println(p.cordX+","+p.cordY);

        if(Mx != 0){
            for(int x = 0; x < MainMenu.stars; x++){
                s[x].setMove((int)Mx,0);
                s[x].update();
            }

        }
        if(My != 0){
            for(int x = 0; x < MainMenu.stars; x++){
                s[x].setMove(0, (int)(My));
                s[x].update();
            }

        }
        /* int movement = step/10;
        step++;
        if(step == 100){
        step = 0;
        }*/

        try{
            if(Greenfoot.getMouseInfo().getButton()==1){

                addObject(beam, 500,300);

            }else if(Greenfoot.getMouseInfo().getButton()==3){
                beam.dettach();
                removeObject(beam);

            }
        }catch(NullPointerException e){
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public static int getTrueX(){
        return UlX;
    }

    public static int getTrueY(){
        return UlY;
    }

    public static Attachable[] returnEverySingleAttachableInThisWorld(){//RESAITW
        Attachable[] TheArrayWithAnEvenLongerLastNameWhichHappensToBeContrarez = new Attachable[2+addons.size()];
        TheArrayWithAnEvenLongerLastNameWhichHappensToBeContrarez[0] = (Attachable)p;
        TheArrayWithAnEvenLongerLastNameWhichHappensToBeContrarez[1] = (Attachable)piece;
        for(int x = 0; x < addons.size(); x++){
            TheArrayWithAnEvenLongerLastNameWhichHappensToBeContrarez[x+2] = addons.get(x);
         }
        return TheArrayWithAnEvenLongerLastNameWhichHappensToBeContrarez;
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
    }
}
